<?php
/**
 * Qx Excel日历
 * 
 * @author Wu Bai Qing<wbqyyicx@gmail.com>
 * @version $Id: CToday.php 3 2011-09-03 02:47:36Z wbqyyicx $
 * @copyright Copyright (c) 2011 Qx
 */

$get_date = isset($_GET['date']) ? $_GET['date'] : date('Y-m-d');

$date = getdate(strtotime($get_date));

$start_month = getdate(mktime(0,0,0,$date['mon'],1,$date['year']));

$end_month = getdate(mktime(0,0,0,$date['mon'] + 1,1,$date['year']) - 1);


$new_calendar = array();
$calendar_tpl = array('0'=>'','1'=>'','2'=>'','3'=>'','4'=>'','5'=>'','6'=>'');
$j = 0;

for($i = 0 ; $i < $end_month['mday']; $i++)
{
	
	if (!isset($new_calendar[$j]))
	{
        $new_calendar[$j] = $calendar_tpl;
    }
    
    $new_calendar[$j][($i+$start_month['wday'])%7] = $i + 1;
    
    if ($new_calendar[$j][6])
    {
        $j++;
    }
    
}

$previous = date('Y-m-d',$start_month['0']-1);
$next = date('Y-m-d',$end_month['0']+86400);











$html = '<table border="1">';
$html .= '<tr>';
$html .= '<td><a href="' . $_SERVER['PHP_SELF'] . '?date=' . $previous. '">-</a></td>';
$html .= '<td colspan="5">' . $date['year'] . ';' . $get_date['month'] . '</td>';
$html .= '<td><a href="' . $_SERVER['PHP_SELF'] . '?date=' . $next . '">+</a></td>';
$html .= '</tr>';

foreach($new_calendar as $key => $val)
{
	$html.= '<tr>';
	foreach ($val as $k => $v)
	{
		if($v)
		{
			if($v == $get_date['mday'])
			{
				$html.='<td><font color=red>'.$v.'</font></td>';
			}
			else 
			{
				$html.='<td>'.$v.'</td>';
			}
		}
		else
		{
			 $html .= '<td>&nbsp;</td>';
		}
	}
	 $html.='</tr>';
}

$html.='</table>';
echo $html;
